package com.yhl.kafka;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author: yanghl
 * @description: TODO
 * @date: 2024/3/26 16:31
 */
@Component
@Slf4j
public class KafkaConsumer {
    private final String topicName = "hub-topic-city-01";
    /** 先注释，防止搞其他功能时一直刷kafka日志
    @KafkaListener(
            topics = {topicName},
            groupId = "hub-topic-city-01-group")
    **/
    public void consumeMsg(ConsumerRecord<?, ?> record) {
        try {
            //1.从ConsumerRecord中获取消费数据
            String originalMsg = (String) record.value();
            log.info("从Kafka中消费的原始数据: " + originalMsg);
            //2.把消费数据转换为DTO对象
            // CityDTO cityDTO = JSONUtil.toBean(originalMsg, CityDTO.class);
            log.info("消费数据转换为DTO对象: " + originalMsg);
        } catch (Exception e) {
            log.info("Consumer消费Topic异常.");
            e.printStackTrace();
        }
    }
}

